/**
 * Created by Demon on 2015/5/8.
 */
var NaturePage = function(){
    function showBlockUI(message) {
        Metronic.blockUI({
            boxed: true,
            message: message
        });
        window.setTimeout(function () {
            Metronic.unblockUI();
        }, 500);
    };

    return{
      init:function(){

          $("#save").on('click',function() {
              $.get(basePath + "nature/save",function() {});
          });

          $("#getNa").on('click',function() {
             $("#tbody tr").remove();
             var cityName = $("#cityName").val().trim();
              $.ajax({
                  url:basePath+"nature/getHelp/cityName",
                  dataType:"json",
                  type:"post",
                  data:"cityName="+ cityName,
                  success:function(data){
                      $("#tbody").append(
                          "<tr>" +
                              "<td>" +data.cityName + "</td>" +
                              "<td>" +data.altitude + "</td>" +
                              "<td>" +data.hpaSummer + "</td>" +
                              "<td>" +data.hpaWinter +"</td>" +
                              "<td>" +data.avgTep + "</td>" +
                              "<td>" +data.outCulTep + "</td>" +
                              "<td>" +data.dayLowTep+ "</td>" +
                              "<td>" +data.relHum +"</td>" +
                              "</tr>"
                      );
                  }
              })
          });

          $("#getNaAll").on('click',function(){
              $("#tbody tr").remove();
              $.get(basePath + "nature/getNaAll",function(data){
                  for(var i=0;i<=data.length;i++){
//                      $("#cityName1").html(data[i].cityName);
//                      $("#altitudeSearch").html(data[i].altitude);
//                      $("#hpaSumer").html(data[i].hpaSummer);
//                      $("#hpaWinter").html(data[i].hpaWinter);
//                      $("#avgTep").html(data[i].avgTep);
//                      $("#outCulTep").html(data[i].outCulTep);
//                      $("#dayLowTep").html(data[i].dayLowTep);
//                      $("#relHum").html(data[i].relHum);
                     $("#tbody").append(
                         "<tr>" +
                             "<td>" +data[i].cityName + "</td>" +
                             "<td>" +data[i].altitude + "</td>" +
                             "<td>" +data[i].hpaSummer + "</td>" +
                             "<td>" +data[i].hpaWinter +"</td>" +
                             "<td>" +data[i].avgTep + "</td>" +
                             "<td>" +data[i].outCulTep + "</td>" +
                             "<td>" +data[i].dayLowTep+ "</td>" +
                             "<td>" +data[i].relHum +"</td>" +
                          "</tr>"
                      );
                 }
              });
          });


          $("#altitude").blur(function(){
              var $obj = $("#altitude");
              var altitude = $obj.val();
              validate.init($obj);

              if(altitude!=""){
                  if(validate.isNotNumber(altitude)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateAltitude",
                      dataType:"json",
                      type:"post",
                      data:"altitude=" + altitude
                  })
              } else{
                  validate.invalid();
                  return;
              }

              //如果海拔高度和年平均气温都有值，则计算当地大气压
              var annualAverageTemperature = $("#annualAverageTemperature").val();
              if (validate.isNotNumber(altitude) || validate.isNotNumber(annualAverageTemperature))
                  return;
//              $.ajax({
//                  "url":"nature/calculateLocalPressureAndOther",
//                  type:"post",
//                  dataType:"json",
//                  success:function(data){
//                      $("#localPressure").val(data.localPressure);
//                      $("#localPressureOther").val(data.LocalPressureOther);
//                  }
//              })

          })

          $("#annualAverageTemperature").blur(function(){
              var $obj = $("#annualAverageTemperature");
              var annualAverageTemperature = $obj.val();
              validate.init($obj);

              if(annualAverageTemperature!=""){
                  if(validate.isNotNumber(annualAverageTemperature)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateAnnualAverageTemperature",
                      dataType:"json",
                      type:"post",
                      data:"annualAverageTemperature=" + annualAverageTemperature
                  })
              } else{
                  validate.invalid();
                  return;
              }

              //如果海拔高度和年平均气温都有值，则计算当地大气压
              var altitude = $("#altitude").val();
              if (validate.isNotNumber(altitude) || validate.isNotNumber(annualAverageTemperature))
                  return;
              $.ajax({
                  "url":"nature/calculateLocalPressureAndOther",
                  type:"post",
                  dataType:"json",
                  success:function(data){
                      $("#localPressure").val(data.localPressure);
                      $("#localPressureOther").val(data.LocalPressureOther);
                  }
              })
          })

          $("#outdoorCalculatedTemperature").blur(function(){
              var $obj = $("#outdoorCalculatedTemperature");
              var outdoorCalculatedTemperature = $obj.val();
              validate.init($obj);

              if(outdoorCalculatedTemperature!=""){
                  if(validate.isNotNumber(outdoorCalculatedTemperature)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateOutdoorCalculatedTemperature",
                      dataType:"json",
                      type:"post",
                      data:"outdoorCalculatedTemperature=" + outdoorCalculatedTemperature
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#annualAverageRelativeHumidity").blur(function(){
              var $obj = $("#annualAverageRelativeHumidity");
              var annualAverageRelativeHumidity = $obj.val();
              validate.init($obj);

              if(annualAverageRelativeHumidity!=""){
                  if(validate.isNotNumber(annualAverageRelativeHumidity)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateAnnualAverageRelativeHumidity",
                      dataType:"json",
                      type:"post",
                      data:"annualAverageRelativeHumidity=" + annualAverageRelativeHumidity
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#minDailyAverageTemperature").blur(function(){
              var $obj = $("#minDailyAverageTemperature");
              var minDailyAverageTemperature = $obj.val();
              validate.init($obj);

              if(minDailyAverageTemperature!=""){
                  if(validate.isNotNumber(minDailyAverageTemperature)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateMinDailyAverageTemperature",
                      dataType:"json",
                      type:"post",
                      data:"minDailyAverageTemperature=" + minDailyAverageTemperature
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#localPressure").blur(function(){
              var $obj = $("#localPressure");
              var localPressure = $obj.val();
              validate.init($obj);

              if(localPressure!=""){
                  if(validate.isNotNumber(localPressure)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateLocalPressure",
                      dataType:"json",
                      type:"post",
                      data:"localPressure=" + localPressure
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#localPressureOther").blur(function(){
              var $obj = $("#localPressureOther");
              var localPressureOther = $obj.val();
              validate.init($obj);

              if(localPressureOther!=""){
                  if(validate.isNotNumber(localPressureOther)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateLocalPressureOther",
                      dataType:"json",
                      type:"post",
                      data:"localPressureOther=" + localPressureOther
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#summerOutdoorCalculateTemperature").blur(function(){
              var $obj = $("#summerOutdoorCalculateTemperature");
              var summerOutdoorCalculateTemperature = $obj.val();
              validate.init($obj);

              if(summerOutdoorCalculateTemperature!=""){
                  if(validate.isNotNumber(summerOutdoorCalculateTemperature)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateSummerOutdoorCalculateTemperature",
                      dataType:"json",
                      type:"post",
                      data:"summerOutdoorCalculateTemperature=" + summerOutdoorCalculateTemperature
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#annualAverageAirMoisture").blur(function(){
              var $obj = $("#annualAverageAirMoisture");
              var annualAverageAirMoisture = $obj.val();
              validate.init($obj);

              if(annualAverageAirMoisture!=""){
                  if(validate.isNotNumber(annualAverageAirMoisture)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateAnnualAverageAirMoisture",
                      dataType:"json",
                      type:"post",
                      data:"annualAverageAirMoisture=" + annualAverageAirMoisture
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#localDensityMoistAir").blur(function(){
              var $obj = $("#localDensityMoistAir");
              var localDensityMoistAir = $obj.val();
              validate.init($obj);

              if(localDensityMoistAir!=""){
                  if(validate.isNotNumber(localDensityMoistAir)){
                      validate.invalid();
                      return;
                  }
                  validate.valid();
                  $.ajax({
                      url: basePath + "nature/updateLocalDensityMoistAir",
                      dataType:"json",
                      type:"post",
                      data:"localDensityMoistAir=" + localDensityMoistAir
                  })
              } else{
                  validate.invalid();
                  return;
              }
          })

          $("#computerAirMoisture").click(function () {
              var nature = {
              }
              showBlockUI("计算中..");
              $.ajax({
                  url: basePath + "nature/computerAirMoisture",
                  contentType: "application/json;charset=utf-8",
                  data: JSON.stringify(nature),
                  dataType:"json",
                  type: "post",
                  success: function (data) {
                      $("#annualAverageAirMoisture").val(data.returnMsg);
                  }
              })
          })

          $("#computerLocalDensityMoistAir").click(function () {
              var nature = {
              }
              showBlockUI("计算中..");
              $.ajax({
                  url: basePath + "nature/computerLocalDensityMoistAir",
                  contentType: "application/json;charset=utf-8",
                  data: JSON.stringify(nature),
                  dataType:"json",
                  type: "post",
                  success: function (data) {
                      $("#localDensityMoistAir").val(data.returnMsg);
                  }
              })
          })

      },

        recover:function(){
            $.get(basePath + "nature/getNature",function(data){
                $("#altitude").val(getValidRecoverVal(data.altitude));
                $("#annualAverageTemperature").val(getValidRecoverVal(data.annualAverageTemperature));
                $("#outdoorCalculatedTemperature").val(getValidRecoverVal(data.outdoorCalculatedTemperature));
                $("#annualAverageRelativeHumidity").val(getValidRecoverVal(data.annualAverageRelativeHumidity));
                $("#minDailyAverageTemperature").val(getValidRecoverVal(data.minDailyAverageTemperature));
                $("#localPressure").val(getValidRecoverVal(data.localPressure));
                $("#localPressureOther").val(getValidRecoverVal(data.localPressureOther));
                $("#summerOutdoorCalculateTemperature").val(getValidRecoverVal(data.summerOutdoorCalculateTemperature));
                $("#annualAverageAirMoisture").val(getValidRecoverVal(data.annualAverageAirMoisture));
                $("#localDensityMoistAir").val(getValidRecoverVal(data.localDensityMoistAir));

            });
        }

    }
}();
